<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LANGUAGE Safe #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE CPP #-}</span><span>
</span><span id="line-3"></span><span class="hs-pragma">{-# LANGUAGE StandaloneDeriving #-}</span><span>
</span><span id="line-4"></span><span>
</span><span id="line-5"></span><span class="hs-comment">-------------------------------------------------------------------------------</span><span>
</span><span id="line-6"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-7"></span><span class="hs-comment">-- Module      :  System.Timeout</span><span>
</span><span id="line-8"></span><span class="hs-comment">-- Copyright   :  (c) The University of Glasgow 2007</span><span>
</span><span id="line-9"></span><span class="hs-comment">-- License     :  BSD-style (see the file libraries/base/LICENSE)</span><span>
</span><span id="line-10"></span><span class="hs-comment">--</span><span>
</span><span id="line-11"></span><span class="hs-comment">-- Maintainer  :  libraries@haskell.org</span><span>
</span><span id="line-12"></span><span class="hs-comment">-- Stability   :  experimental</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- Portability :  non-portable</span><span>
</span><span id="line-14"></span><span class="hs-comment">--</span><span>
</span><span id="line-15"></span><span class="hs-comment">-- Attach a timeout event to arbitrary 'IO' computations.</span><span>
</span><span id="line-16"></span><span class="hs-comment">--</span><span>
</span><span id="line-17"></span><span class="hs-comment">-------------------------------------------------------------------------------</span><span>
</span><span id="line-18"></span><span class="hs-comment">-- TODO: Inspect is still suitable.</span><span>
</span><span id="line-19"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">System.Timeout</span><span> </span><span class="hs-special">(</span><span> </span><span class="annot"><a href="System.Timeout.html#Timeout"><span class="hs-identifier">Timeout</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="System.Timeout.html#timeout"><span class="hs-identifier">timeout</span></a></span><span> </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span class="hs-cpp">

#if !defined(mingw32_HOST_OS)
</span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Control.Monad</span><span>
</span><span id="line-23"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Event</span><span>           </span><span class="hs-special">(</span><span class="hs-identifier">getSystemTimerManager</span><span class="hs-special">,</span><span>
</span><span id="line-24"></span><span>                            </span><span class="hs-identifier">registerTimeout</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">unregisterTimeout</span><span class="hs-special">)</span><span class="hs-cpp">
#endif
</span><span>
</span><span id="line-27"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Control.Concurrent.html"><span class="hs-identifier">Control.Concurrent</span></a></span><span>
</span><span id="line-28"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Control.Exception.html"><span class="hs-identifier">Control.Exception</span></a></span><span>   </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Exception.Type.html#Exception"><span class="hs-identifier">Exception</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Exception.Base.html#handleJust"><span class="hs-identifier">handleJust</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Control.Exception.Base.html#bracket"><span class="hs-identifier">bracket</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-29"></span><span>                            </span><span class="annot"><a href="GHC.IO.html#uninterruptibleMask_"><span class="hs-identifier">uninterruptibleMask_</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-30"></span><span>                            </span><span class="annot"><a href="GHC.IO.Exception.html#asyncExceptionToException"><span class="hs-identifier">asyncExceptionToException</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-31"></span><span>                            </span><span class="annot"><a href="GHC.IO.Exception.html#asyncExceptionFromException"><span class="hs-identifier">asyncExceptionFromException</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-32"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Data.Unique.html"><span class="hs-identifier">Data.Unique</span></a></span><span>         </span><span class="hs-special">(</span><span class="annot"><a href="Data.Unique.html#Unique"><span class="hs-identifier">Unique</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="Data.Unique.html#newUnique"><span class="hs-identifier">newUnique</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-33"></span><span>
</span><span id="line-34"></span><span class="hs-comment">-- An internal type that is thrown as a dynamic exception to</span><span>
</span><span id="line-35"></span><span class="hs-comment">-- interrupt the running IO computation when the timeout has</span><span>
</span><span id="line-36"></span><span class="hs-comment">-- expired.</span><span>
</span><span id="line-37"></span><span>
</span><span id="line-38"></span><span class="hs-comment">-- | An exception thrown to a thread by 'timeout' to interrupt a timed-out</span><span>
</span><span id="line-39"></span><span class="hs-comment">-- computation.</span><span>
</span><span id="line-40"></span><span class="hs-comment">--</span><span>
</span><span id="line-41"></span><span class="hs-comment">-- @since 4.0</span><span>
</span><span id="line-42"></span><span class="hs-keyword">newtype</span><span> </span><span id="Timeout"><span class="annot"><a href="System.Timeout.html#Timeout"><span class="hs-identifier hs-var">Timeout</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span id="Timeout"><span class="annot"><a href="System.Timeout.html#Timeout"><span class="hs-identifier hs-var">Timeout</span></a></span></span><span> </span><span class="annot"><a href="Data.Unique.html#Unique"><span class="hs-identifier hs-type">Unique</span></a></span><span> </span><span class="hs-keyword">deriving</span><span> </span><span id="local-6989586621679565563"><span id="local-6989586621679565568"><span class="annot"><span class="annottext">Timeout -&gt; Timeout -&gt; Bool
(Timeout -&gt; Timeout -&gt; Bool)
-&gt; (Timeout -&gt; Timeout -&gt; Bool) -&gt; Eq Timeout
forall a. (a -&gt; a -&gt; Bool) -&gt; (a -&gt; a -&gt; Bool) -&gt; Eq a
/= :: Timeout -&gt; Timeout -&gt; Bool
$c/= :: Timeout -&gt; Timeout -&gt; Bool
== :: Timeout -&gt; Timeout -&gt; Bool
$c== :: Timeout -&gt; Timeout -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#Eq"><span class="hs-identifier hs-var hs-var hs-var hs-var hs-var hs-var">Eq</span></a></span></span></span><span>
</span><span id="line-43"></span><span>
</span><span id="line-44"></span><span class="hs-comment">-- | @since 4.0</span><span>
</span><span id="line-45"></span><span class="hs-keyword">instance</span><span> </span><span id="local-6989586621679565554"><span id="local-6989586621679565557"><span class="annot"><a href="GHC.Show.html#Show"><span class="hs-identifier hs-type">Show</span></a></span><span> </span><span class="annot"><a href="System.Timeout.html#Timeout"><span class="hs-identifier hs-type">Timeout</span></a></span></span></span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-46"></span><span>    </span><span id="local-6989586621679565552"><span class="annot"><span class="annottext">show :: Timeout -&gt; String
</span><a href="GHC.Show.html#show"><span class="hs-identifier hs-var hs-var hs-var hs-var">show</span></a></span></span><span> </span><span class="annot"><span class="annottext">Timeout
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">String
</span><span class="hs-string">&quot;&lt;&lt;timeout&gt;&gt;&quot;</span></span><span>
</span><span id="line-47"></span><span>
</span><span id="line-48"></span><span class="hs-comment">-- Timeout is a child of SomeAsyncException</span><span>
</span><span id="line-49"></span><span class="hs-comment">-- | @since 4.7.0.0</span><span>
</span><span id="line-50"></span><span class="hs-keyword">instance</span><span> </span><span id="local-6989586621679565543"><span class="annot"><a href="GHC.Exception.Type.html#Exception"><span class="hs-identifier hs-type">Exception</span></a></span><span> </span><span class="annot"><a href="System.Timeout.html#Timeout"><span class="hs-identifier hs-type">Timeout</span></a></span></span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-51"></span><span>  </span><span id="local-6989586621679565540"><span class="annot"><span class="annottext">toException :: Timeout -&gt; SomeException
</span><a href="GHC.Exception.Type.html#toException"><span class="hs-identifier hs-var hs-var hs-var hs-var">toException</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Timeout -&gt; SomeException
forall e. Exception e =&gt; e -&gt; SomeException
</span><a href="GHC.IO.Exception.html#asyncExceptionToException"><span class="hs-identifier hs-var">asyncExceptionToException</span></a></span><span>
</span><span id="line-52"></span><span>  </span><span id="local-6989586621679565537"><span class="annot"><span class="annottext">fromException :: SomeException -&gt; Maybe Timeout
</span><a href="GHC.Exception.Type.html#fromException"><span class="hs-identifier hs-var hs-var hs-var hs-var">fromException</span></a></span></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">SomeException -&gt; Maybe Timeout
forall e. Exception e =&gt; SomeException -&gt; Maybe e
</span><a href="GHC.IO.Exception.html#asyncExceptionFromException"><span class="hs-identifier hs-var">asyncExceptionFromException</span></a></span><span>
</span><span id="line-53"></span><span>
</span><span id="line-54"></span><span class="hs-comment">-- |Wrap an 'IO' computation to time out and return @Nothing@ in case no result</span><span>
</span><span id="line-55"></span><span class="hs-comment">-- is available within @n@ microseconds (@1\/10^6@ seconds). In case a result</span><span>
</span><span id="line-56"></span><span class="hs-comment">-- is available before the timeout expires, @Just a@ is returned. A negative</span><span>
</span><span id="line-57"></span><span class="hs-comment">-- timeout interval means \&quot;wait indefinitely\&quot;. When specifying long timeouts,</span><span>
</span><span id="line-58"></span><span class="hs-comment">-- be careful not to exceed @maxBound :: Int@.</span><span>
</span><span id="line-59"></span><span class="hs-comment">--</span><span>
</span><span id="line-60"></span><span class="hs-comment">-- &gt;&gt;&gt; timeout 1000000 (threadDelay 1000 *&gt; pure &quot;finished on time&quot;)</span><span>
</span><span id="line-61"></span><span class="hs-comment">-- Just &quot;finished on time&quot;</span><span>
</span><span id="line-62"></span><span class="hs-comment">--</span><span>
</span><span id="line-63"></span><span class="hs-comment">-- &gt;&gt;&gt; timeout 10000 (threadDelay 100000 *&gt; pure &quot;finished on time&quot;)</span><span>
</span><span id="line-64"></span><span class="hs-comment">-- Nothing</span><span>
</span><span id="line-65"></span><span class="hs-comment">--</span><span>
</span><span id="line-66"></span><span class="hs-comment">-- The design of this combinator was guided by the objective that @timeout n f@</span><span>
</span><span id="line-67"></span><span class="hs-comment">-- should behave exactly the same as @f@ as long as @f@ doesn't time out. This</span><span>
</span><span id="line-68"></span><span class="hs-comment">-- means that @f@ has the same 'myThreadId' it would have without the timeout</span><span>
</span><span id="line-69"></span><span class="hs-comment">-- wrapper. Any exceptions @f@ might throw cancel the timeout and propagate</span><span>
</span><span id="line-70"></span><span class="hs-comment">-- further up. It also possible for @f@ to receive exceptions thrown to it by</span><span>
</span><span id="line-71"></span><span class="hs-comment">-- another thread.</span><span>
</span><span id="line-72"></span><span class="hs-comment">--</span><span>
</span><span id="line-73"></span><span class="hs-comment">-- A tricky implementation detail is the question of how to abort an @IO@</span><span>
</span><span id="line-74"></span><span class="hs-comment">-- computation. This combinator relies on asynchronous exceptions internally</span><span>
</span><span id="line-75"></span><span class="hs-comment">-- (namely throwing the computation the 'Timeout' exception).  The technique</span><span>
</span><span id="line-76"></span><span class="hs-comment">-- works very well for computations executing inside of the Haskell runtime</span><span>
</span><span id="line-77"></span><span class="hs-comment">-- system, but it doesn't work at all for non-Haskell code.  Foreign function</span><span>
</span><span id="line-78"></span><span class="hs-comment">-- calls, for example, cannot be timed out with this combinator simply because</span><span>
</span><span id="line-79"></span><span class="hs-comment">-- an arbitrary C function cannot receive asynchronous exceptions. When</span><span>
</span><span id="line-80"></span><span class="hs-comment">-- @timeout@ is used to wrap an FFI call that blocks, no timeout event can be</span><span>
</span><span id="line-81"></span><span class="hs-comment">-- delivered until the FFI call returns, which pretty much negates the purpose</span><span>
</span><span id="line-82"></span><span class="hs-comment">-- of the combinator. In practice, however, this limitation is less severe than</span><span>
</span><span id="line-83"></span><span class="hs-comment">-- it may sound. Standard I\/O functions like 'System.IO.hGetBuf',</span><span>
</span><span id="line-84"></span><span class="hs-comment">-- 'System.IO.hPutBuf', Network.Socket.accept, or 'System.IO.hWaitForInput'</span><span>
</span><span id="line-85"></span><span class="hs-comment">-- appear to be blocking, but they really don't because the runtime system uses</span><span>
</span><span id="line-86"></span><span class="hs-comment">-- scheduling mechanisms like @select(2)@ to perform asynchronous I\/O, so it</span><span>
</span><span id="line-87"></span><span class="hs-comment">-- is possible to interrupt standard socket I\/O or file I\/O using this</span><span>
</span><span id="line-88"></span><span class="hs-comment">-- combinator.</span><span>
</span><span id="line-89"></span><span class="hs-comment">---</span><span>
</span><span id="line-90"></span><span class="hs-comment">-- Note that 'timeout' cancels the computation by throwing it the 'Timeout'</span><span>
</span><span id="line-91"></span><span class="hs-comment">-- exception. Consequently blanket exception handlers (e.g. catching</span><span>
</span><span id="line-92"></span><span class="hs-comment">-- 'SomeException') within the computation will break the timeout behavior.</span><span>
</span><span id="line-93"></span><span id="local-6989586621679565608"><span class="annot"><a href="System.Timeout.html#timeout"><span class="hs-identifier hs-type">timeout</span></a></span><span> </span><span class="hs-glyph">::</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#Int"><span class="hs-identifier hs-type">Int</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679565608"><span class="hs-identifier hs-type">a</span></a></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><a href="../../ghc-prim/src/GHC.Types.html#IO"><span class="hs-identifier hs-type">IO</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><a href="GHC.Maybe.html#Maybe"><span class="hs-identifier hs-type">Maybe</span></a></span><span> </span><span class="annot"><a href="#local-6989586621679565608"><span class="hs-identifier hs-type">a</span></a></span><span class="hs-special">)</span></span><span>
</span><span id="line-94"></span><span id="timeout"><span class="annot"><span class="annottext">timeout :: forall a. Int -&gt; IO a -&gt; IO (Maybe a)
</span><a href="System.Timeout.html#timeout"><span class="hs-identifier hs-var hs-var">timeout</span></a></span></span><span> </span><span id="local-6989586621679565515"><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679565515"><span class="hs-identifier hs-var">n</span></a></span></span><span> </span><span id="local-6989586621679565514"><span class="annot"><span class="annottext">IO a
</span><a href="#local-6989586621679565514"><span class="hs-identifier hs-var">f</span></a></span></span><span>
</span><span id="line-95"></span><span>    </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679565515"><span class="hs-identifier hs-var">n</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Bool
forall a. Ord a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3C"><span class="hs-operator hs-var">&lt;</span></a></span><span>  </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">(a -&gt; Maybe a) -&gt; IO a -&gt; IO (Maybe a)
forall (f :: * -&gt; *) a b. Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
</span><a href="GHC.Base.html#fmap"><span class="hs-identifier hs-var">fmap</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; Maybe a
forall a. a -&gt; Maybe a
</span><a href="GHC.Maybe.html#Just"><span class="hs-identifier hs-var">Just</span></a></span><span> </span><span class="annot"><span class="annottext">IO a
</span><a href="#local-6989586621679565514"><span class="hs-identifier hs-var">f</span></a></span><span>
</span><span id="line-96"></span><span>    </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679565515"><span class="hs-identifier hs-var">n</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; Int -&gt; Bool
forall a. Eq a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3D%3D"><span class="hs-operator hs-var">==</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><span class="hs-number">0</span></span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="annot"><span class="annottext">Maybe a -&gt; IO (Maybe a)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">Maybe a
forall a. Maybe a
</span><a href="GHC.Maybe.html#Nothing"><span class="hs-identifier hs-var">Nothing</span></a></span><span class="hs-cpp">
#if !defined(mingw32_HOST_OS)
</span><span>    </span><span class="hs-glyph">|</span><span> </span><span class="hs-identifier">rtsSupportsBoundThreads</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-99"></span><span>        </span><span class="hs-comment">-- In the threaded RTS, we use the Timer Manager to delay the</span><span>
</span><span id="line-100"></span><span>        </span><span class="hs-comment">-- (fairly expensive) 'forkIO' call until the timeout has expired.</span><span>
</span><span id="line-101"></span><span>        </span><span class="hs-comment">--</span><span>
</span><span id="line-102"></span><span>        </span><span class="hs-comment">-- An additional thread is required for the actual delivery of</span><span>
</span><span id="line-103"></span><span>        </span><span class="hs-comment">-- the Timeout exception because killThread (or another throwTo)</span><span>
</span><span id="line-104"></span><span>        </span><span class="hs-comment">-- is the only way to reliably interrupt a throwTo in flight.</span><span>
</span><span id="line-105"></span><span>        </span><span class="hs-identifier">pid</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">myThreadId</span><span>
</span><span id="line-106"></span><span>        </span><span class="hs-identifier">ex</span><span>  </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">fmap</span><span> </span><span class="hs-identifier">Timeout</span><span> </span><span class="hs-identifier">newUnique</span><span>
</span><span id="line-107"></span><span>        </span><span class="hs-identifier">tm</span><span>  </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">getSystemTimerManager</span><span>
</span><span id="line-108"></span><span>        </span><span class="hs-comment">-- 'lock' synchronizes the timeout handler and the main thread:</span><span>
</span><span id="line-109"></span><span>        </span><span class="hs-comment">--  * the main thread can disable the handler by writing to 'lock';</span><span>
</span><span id="line-110"></span><span>        </span><span class="hs-comment">--  * the handler communicates the spawned thread's id through 'lock'.</span><span>
</span><span id="line-111"></span><span>        </span><span class="hs-comment">-- These two cases are mutually exclusive.</span><span>
</span><span id="line-112"></span><span>        </span><span class="hs-identifier">lock</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">newEmptyMVar</span><span>
</span><span id="line-113"></span><span>        </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">handleTimeout</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-114"></span><span>                </span><span class="hs-identifier">v</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">isEmptyMVar</span><span> </span><span class="hs-identifier">lock</span><span>
</span><span id="line-115"></span><span>                </span><span class="hs-identifier">when</span><span> </span><span class="hs-identifier">v</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">void</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">forkIOWithUnmask</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">unmask</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">unmask</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-116"></span><span>                    </span><span class="hs-identifier">v2</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">tryPutMVar</span><span> </span><span class="hs-identifier">lock</span><span> </span><span class="hs-operator">=&lt;&lt;</span><span> </span><span class="hs-identifier">myThreadId</span><span>
</span><span id="line-117"></span><span>                    </span><span class="hs-identifier">when</span><span> </span><span class="hs-identifier">v2</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">throwTo</span><span> </span><span class="hs-identifier">pid</span><span> </span><span class="hs-identifier">ex</span><span>
</span><span id="line-118"></span><span>            </span><span class="hs-identifier">cleanupTimeout</span><span> </span><span class="hs-identifier">key</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">uninterruptibleMask_</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-119"></span><span>                </span><span class="hs-identifier">v</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">tryPutMVar</span><span> </span><span class="hs-identifier">lock</span><span> </span><span class="hs-identifier">undefined</span><span>
</span><span id="line-120"></span><span>                </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">v</span><span> </span><span class="hs-keyword">then</span><span> </span><span class="hs-identifier">unregisterTimeout</span><span> </span><span class="hs-identifier">tm</span><span> </span><span class="hs-identifier">key</span><span>
</span><span id="line-121"></span><span>                     </span><span class="hs-keyword">else</span><span> </span><span class="hs-identifier">takeMVar</span><span> </span><span class="hs-identifier">lock</span><span> </span><span class="hs-operator">&gt;&gt;=</span><span> </span><span class="hs-identifier">killThread</span><span>
</span><span id="line-122"></span><span>        </span><span class="hs-identifier">handleJust</span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">e</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">e</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">ex</span><span> </span><span class="hs-keyword">then</span><span> </span><span class="hs-identifier">Just</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">else</span><span> </span><span class="hs-identifier">Nothing</span><span class="hs-special">)</span><span>
</span><span id="line-123"></span><span>                   </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">Nothing</span><span class="hs-special">)</span><span>
</span><span id="line-124"></span><span>                   </span><span class="hs-special">(</span><span class="hs-identifier">bracket</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">registerTimeout</span><span> </span><span class="hs-identifier">tm</span><span> </span><span class="hs-identifier">n</span><span> </span><span class="hs-identifier">handleTimeout</span><span class="hs-special">)</span><span>
</span><span id="line-125"></span><span>                            </span><span class="hs-identifier">cleanupTimeout</span><span>
</span><span id="line-126"></span><span>                            </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">fmap</span><span> </span><span class="hs-identifier">Just</span><span> </span><span class="hs-identifier">f</span><span class="hs-special">)</span><span class="hs-special">)</span><span class="hs-cpp">
#endif
</span><span>    </span><span class="hs-glyph">|</span><span> </span><span class="annot"><span class="annottext">Bool
</span><a href="GHC.Base.html#otherwise"><span class="hs-identifier hs-var">otherwise</span></a></span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-129"></span><span>        </span><span id="local-6989586621679565512"><span class="annot"><span class="annottext">ThreadId
</span><a href="#local-6989586621679565512"><span class="hs-identifier hs-var">pid</span></a></span></span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">IO ThreadId
</span><a href="GHC.Conc.Sync.html#myThreadId"><span class="hs-identifier hs-var">myThreadId</span></a></span><span>
</span><span id="line-130"></span><span>        </span><span id="local-6989586621679565510"><span class="annot"><span class="annottext">Timeout
</span><a href="#local-6989586621679565510"><span class="hs-identifier hs-var">ex</span></a></span></span><span>  </span><span class="hs-glyph">&lt;-</span><span> </span><span class="annot"><span class="annottext">(Unique -&gt; Timeout) -&gt; IO Unique -&gt; IO Timeout
forall (f :: * -&gt; *) a b. Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
</span><a href="GHC.Base.html#fmap"><span class="hs-identifier hs-var">fmap</span></a></span><span> </span><span class="annot"><span class="annottext">Unique -&gt; Timeout
</span><a href="System.Timeout.html#Timeout"><span class="hs-identifier hs-var">Timeout</span></a></span><span> </span><span class="annot"><span class="annottext">IO Unique
</span><a href="Data.Unique.html#newUnique"><span class="hs-identifier hs-var">newUnique</span></a></span><span>
</span><span id="line-131"></span><span>        </span><span class="annot"><span class="annottext">(Timeout -&gt; Maybe ())
-&gt; (() -&gt; IO (Maybe a)) -&gt; IO (Maybe a) -&gt; IO (Maybe a)
forall e b a.
Exception e =&gt;
(e -&gt; Maybe b) -&gt; (b -&gt; IO a) -&gt; IO a -&gt; IO a
</span><a href="Control.Exception.Base.html#handleJust"><span class="hs-identifier hs-var">handleJust</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span id="local-6989586621679565509"><span class="annot"><span class="annottext">Timeout
</span><a href="#local-6989586621679565509"><span class="hs-identifier hs-var">e</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">if</span><span> </span><span class="annot"><span class="annottext">Timeout
</span><a href="#local-6989586621679565509"><span class="hs-identifier hs-var">e</span></a></span><span> </span><span class="annot"><span class="annottext">Timeout -&gt; Timeout -&gt; Bool
forall a. Eq a =&gt; a -&gt; a -&gt; Bool
</span><a href="../../ghc-prim/src/GHC.Classes.html#%3D%3D"><span class="hs-operator hs-var">==</span></a></span><span> </span><span class="annot"><span class="annottext">Timeout
</span><a href="#local-6989586621679565510"><span class="hs-identifier hs-var">ex</span></a></span><span> </span><span class="hs-keyword">then</span><span> </span><span class="annot"><span class="annottext">() -&gt; Maybe ()
forall a. a -&gt; Maybe a
</span><a href="GHC.Maybe.html#Just"><span class="hs-identifier hs-var">Just</span></a></span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span> </span><span class="hs-keyword">else</span><span> </span><span class="annot"><span class="annottext">Maybe ()
forall a. Maybe a
</span><a href="GHC.Maybe.html#Nothing"><span class="hs-identifier hs-var">Nothing</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-132"></span><span>                   </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="annot"><span class="annottext">()
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">Maybe a -&gt; IO (Maybe a)
forall (m :: * -&gt; *) a. Monad m =&gt; a -&gt; m a
</span><a href="GHC.Base.html#return"><span class="hs-identifier hs-var">return</span></a></span><span> </span><span class="annot"><span class="annottext">Maybe a
forall a. Maybe a
</span><a href="GHC.Maybe.html#Nothing"><span class="hs-identifier hs-var">Nothing</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-133"></span><span>                   </span><span class="hs-special">(</span><span class="annot"><span class="annottext">IO ThreadId
-&gt; (ThreadId -&gt; IO ())
-&gt; (ThreadId -&gt; IO (Maybe a))
-&gt; IO (Maybe a)
forall a b c. IO a -&gt; (a -&gt; IO b) -&gt; (a -&gt; IO c) -&gt; IO c
</span><a href="Control.Exception.Base.html#bracket"><span class="hs-identifier hs-var">bracket</span></a></span><span> </span><span class="hs-special">(</span><span class="annot"><span class="annottext">((forall a. IO a -&gt; IO a) -&gt; IO ()) -&gt; IO ThreadId
</span><a href="GHC.Conc.Sync.html#forkIOWithUnmask"><span class="hs-identifier hs-var">forkIOWithUnmask</span></a></span><span> </span><span class="annot"><span class="annottext">(((forall a. IO a -&gt; IO a) -&gt; IO ()) -&gt; IO ThreadId)
-&gt; ((forall a. IO a -&gt; IO a) -&gt; IO ()) -&gt; IO ThreadId
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="hs-glyph">\</span><span id="local-6989586621679565507"><span class="annot"><span class="annottext">forall a. IO a -&gt; IO a
</span><a href="#local-6989586621679565507"><span class="hs-identifier hs-var">unmask</span></a></span></span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-134"></span><span>                                 </span><span class="annot"><span class="annottext">IO () -&gt; IO ()
forall a. IO a -&gt; IO a
</span><a href="#local-6989586621679565507"><span class="hs-identifier hs-var">unmask</span></a></span><span> </span><span class="annot"><span class="annottext">(IO () -&gt; IO ()) -&gt; IO () -&gt; IO ()
forall a b. (a -&gt; b) -&gt; a -&gt; b
</span><a href="GHC.Base.html#%24"><span class="hs-operator hs-var">$</span></a></span><span> </span><span class="annot"><span class="annottext">Int -&gt; IO ()
</span><a href="GHC.Conc.IO.html#threadDelay"><span class="hs-identifier hs-var">threadDelay</span></a></span><span> </span><span class="annot"><span class="annottext">Int
</span><a href="#local-6989586621679565515"><span class="hs-identifier hs-var">n</span></a></span><span> </span><span class="annot"><span class="annottext">IO () -&gt; IO () -&gt; IO ()
forall (m :: * -&gt; *) a b. Monad m =&gt; m a -&gt; m b -&gt; m b
</span><a href="GHC.Base.html#%3E%3E"><span class="hs-operator hs-var">&gt;&gt;</span></a></span><span> </span><span class="annot"><span class="annottext">ThreadId -&gt; Timeout -&gt; IO ()
forall e. Exception e =&gt; ThreadId -&gt; e -&gt; IO ()
</span><a href="GHC.Conc.Sync.html#throwTo"><span class="hs-identifier hs-var">throwTo</span></a></span><span> </span><span class="annot"><span class="annottext">ThreadId
</span><a href="#local-6989586621679565512"><span class="hs-identifier hs-var">pid</span></a></span><span> </span><span class="annot"><span class="annottext">Timeout
</span><a href="#local-6989586621679565510"><span class="hs-identifier hs-var">ex</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-135"></span><span>                            </span><span class="hs-special">(</span><span class="annot"><span class="annottext">IO () -&gt; IO ()
forall a. IO a -&gt; IO a
</span><a href="GHC.IO.html#uninterruptibleMask_"><span class="hs-identifier hs-var">uninterruptibleMask_</span></a></span><span> </span><span class="annot"><span class="annottext">(IO () -&gt; IO ()) -&gt; (ThreadId -&gt; IO ()) -&gt; ThreadId -&gt; IO ()
forall b c a. (b -&gt; c) -&gt; (a -&gt; b) -&gt; a -&gt; c
</span><a href="GHC.Base.html#."><span class="hs-operator hs-var">.</span></a></span><span> </span><span class="annot"><span class="annottext">ThreadId -&gt; IO ()
</span><a href="GHC.Conc.Sync.html#killThread"><span class="hs-identifier hs-var">killThread</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-136"></span><span>                            </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="annot"><span class="annottext">ThreadId
</span><span class="hs-identifier">_</span></span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="annot"><span class="annottext">(a -&gt; Maybe a) -&gt; IO a -&gt; IO (Maybe a)
forall (f :: * -&gt; *) a b. Functor f =&gt; (a -&gt; b) -&gt; f a -&gt; f b
</span><a href="GHC.Base.html#fmap"><span class="hs-identifier hs-var">fmap</span></a></span><span> </span><span class="annot"><span class="annottext">a -&gt; Maybe a
forall a. a -&gt; Maybe a
</span><a href="GHC.Maybe.html#Just"><span class="hs-identifier hs-var">Just</span></a></span><span> </span><span class="annot"><span class="annottext">IO a
</span><a href="#local-6989586621679565514"><span class="hs-identifier hs-var">f</span></a></span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-137"></span><span>        </span><span class="hs-comment">-- #7719 explains why we need uninterruptibleMask_ above.</span><span>
</span><span id="line-138"></span></pre></body></html>